﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSQuanLiNhaNuoc : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenLoaiQuanLiNhaNuoc { set; get; }
        public void Edit(int hdId, int quanLiNNID, string ghiChu, DateTime ngayHieuLuc)
        {
            List<NSQuanLiNhaNuoc> lst = (List<NSQuanLiNhaNuoc>)HttpContext.Current.Session["QLNN"];
            NSQuanLiNhaNuoc pc = lst.Where(a => a.QuanLiNhaNuocID.Equals(hdId)).SingleOrDefault();
            pc.LoaiQuanLiNhaNuocID = quanLiNNID;
            pc.GhiChu = ghiChu;
            pc.TenLoaiQuanLiNhaNuoc = common.LoadCommonCodeById(quanLiNNID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                pc.NgayHieuLuc = ngayHieuLuc;
            }

        }
        public void Delete(int hdId)
        {
            List<NSQuanLiNhaNuoc> lst = (List<NSQuanLiNhaNuoc>)HttpContext.Current.Session["QLNN"];
            NSQuanLiNhaNuoc pc = lst.Where(a => a.QuanLiNhaNuocID.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAll(int nhanVienId)
        {
            var query = (from a in db.NSQuanLiNhaNuocs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSQuanLiNhaNuocs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void Update(int nhanVienId, List<NSQuanLiNhaNuoc> lstObject)
        {

            var query = (from a in db.NSQuanLiNhaNuocs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSQuanLiNhaNuocs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

            foreach (NSQuanLiNhaNuoc pc in lstObject)
            {
                NSQuanLiNhaNuoc item = new NSQuanLiNhaNuoc();
                Copy(pc, item);
                db.NSQuanLiNhaNuocs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        public List<NSQuanLiNhaNuoc> LoadData(int nhanVienId)
        {

            var query = (from a in db.NSQuanLiNhaNuocs
                         where a.NhanVienID.Equals(nhanVienId)

                         select a).ToList();
            foreach (NSQuanLiNhaNuoc item in query)
            {
                item.TenLoaiQuanLiNhaNuoc = common.LoadCommonCodeById((int)item.LoaiQuanLiNhaNuocID);
            }

            return query;

        }
        public NSQuanLiNhaNuoc GetItem(int nhanVienId, int quanLiNNID, string ghiChu, DateTime ngayHieuLuc)
        {

            NSQuanLiNhaNuoc hd = new NSQuanLiNhaNuoc();
            hd.QuanLiNhaNuocID = 0 - DateTime.Now.Second;
            hd.GhiChu = ghiChu;
            hd.NhanVienID = nhanVienId;
            hd.LoaiQuanLiNhaNuocID = quanLiNNID;
            hd.TenLoaiQuanLiNhaNuoc = common.LoadCommonCodeById(quanLiNNID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                hd.NgayHieuLuc = ngayHieuLuc;
            }
            return hd;
        }
        private void Copy(NSQuanLiNhaNuoc src, NSQuanLiNhaNuoc des)
        {
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.LoaiQuanLiNhaNuocID = src.LoaiQuanLiNhaNuocID;
            des.TenLoaiQuanLiNhaNuoc = src.TenLoaiQuanLiNhaNuoc;

            if (src.NgayHieuLuc.HasValue)
            {
                des.NgayHieuLuc = src.NgayHieuLuc;
            }
        }
    }
}